<?php

namespace App\Admin\Controllers;

use App\Models\Book;
use App\Models\Word;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

class WordController extends AdminController
{
    protected $title = '单词';

    public function grid()
    {
        // 先引入js和css
        // 播放声音，https://www.schillmania.com/projects/soundmanager2/demo/mp3-player-button/
        // <a href="/path/to/coins.mp3" title="Play &quot;coins&quot;" class="sm2_button">coins.mp3</a>

        $grid = new Grid(Word::with(['book', 'trans']));
        $grid->model()->orderByDesc('id');

        $grid->column('id', 'ID');
        $grid->column('book.name', '单词书名');
        $grid->column('word', '单词');
        $grid->column('trans', '翻译')->display(function () {
            if (!empty($this->trans)) {
                return $this->trans->pluck('tran_cn')->implode(',');
            }
            return '';
        });
        $grid->column('phonetic_us', '美式音标')->display(function () {
            return '<i class="fa fa-volume-up mr-1"></i>' . $this->phonetic_us;
        });
        $grid->column('phonetic_uk', '英式音标')->display(function () {
            return '<i class="fa fa-volume-up mr-1"></i>' . $this->phonetic_us;
        });
//        $grid->column('cover')->image('', 50);
        $grid->column('is_enable', '启用')->switch();

        $grid->filter(function (Grid\Filter $filter) {
            $filter->panel()->expand();
            $filter->like('word', '单词')->width(2);
            $filter->equal('book_id', '单词书名')->select(Book::option())->width(4);
            $filter->scope('enable', '启用')->where('is_enable', 1);
            $filter->scope('disable', '禁用')->where('is_enable', 0);
        });

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            #
        });

        return $grid;
    }

    public function form()
    {
        $form = new Form(Word::with(['trans', 'phrases', 'sentences']));

        $form->image('cover', '封面');
        $form->select('book_id', '单词书名')->options(Book::option());
//        $form->text('word_key');
//        $form->text('word_rank');
        $form->text('word', '单词');
        $form->text('phonetic_us', '美式音标');
        $form->text('phonetic_uk', '英式音标');
        $form->text('speech_us', '美式发音')->help("美式发音参数，如：book&type=2");
        $form->text('speech_uk', '英式发音')->help('英式发音参数，如：book&type=1');
        $form->html('发音接口：https://dict.youdao.com/dictvoice?audio={word}&type={1|2}');
        $form->switch('is_enable', '启用')->default(1);

        $form->table('trans', '翻译', function (Form\NestedForm $table) {
            $table->select('pos', '词性')->options(Word::POS);
            $table->text('tran_cn', '词义');
//            $table->text('tran_en');
        });

        $form->table('phrases', '短语', function (Form\NestedForm $table) {
            $table->text('content', '短语');
            $table->text('tran_cn', '翻译');
        });

        $form->table('sentences', '例句', function (Form\NestedForm $table) {
            $table->text('content', '例句');
            $table->text('tran_cn', '词义');
        });

        return $form;
    }

}
